package im.vinci.server.user.controller;

import com.hankcs.hanlp.HanLP;
import im.vinci.server.common.exceptions.VinciException;
import im.vinci.server.common.exceptions.error.ErrorCode;
import im.vinci.server.security.ApiSecurityLabel;
import im.vinci.server.user.domain.UserAddressBook;
import im.vinci.server.user.domain.UserAttention;
import im.vinci.server.user.domain.UserInfo;
import im.vinci.server.user.service.UserAttentionService;
import im.vinci.server.user.service.UserLoginAndBindDeviceService;
import im.vinci.server.utils.SimilarityUtil;
import im.vinci.server.utils.UserContext;
import im.vinci.server.utils.apiresp.ResultObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

import static im.vinci.server.utils.VinciUtils.nomalizeNickName;
/**
 * 用户关注相关的接口
 * Created by mayuchen on 16/8/2.
 */
@RestController
@RequestMapping(value = "/vinci/user", produces = "application/json;charset=UTF-8")
public class UserAttentionController {

    @Autowired
    private UserAttentionService userAttentionService;      //UserContext.getUserInfo().getId()

    @Autowired
    private UserLoginAndBindDeviceService userService;

    //添加某用户关注
    @RequestMapping(value = "/attention/add", method = RequestMethod.POST)
    @ApiSecurityLabel(isCheckLogin = true)
    public ResultObject AttentionAdd(@RequestParam("attention_uid") long attentionUid){
        return new ResultObject<>(userAttentionService.insertUserAttention(UserContext.getUserInfo().getId(), attentionUid));
    }


    //通过GPS定位试图添加用户
    @RequestMapping(value = "/attention/f2f/add", method = RequestMethod.POST)
    @ApiSecurityLabel(isCheckLogin = true)
    public ResultObject AttentionAddFace2Face(@RequestParam("longitude") float longitude ,
                                              @RequestParam("latitude") float latitude ,
                                              @RequestHeader("sn")String sn){
        return new ResultObject<>(userAttentionService.addAttentionF2F(UserContext.getUserInfo().getId(), sn , longitude, latitude));
    }

    //通过GPS定位试图添加用户
    @RequestMapping(value = "/attention/f2f/cancel", method = RequestMethod.POST)
    @ApiSecurityLabel(isCheckLogin = true)
    public ResultObject AttentionCancelFace2Face(){
        return new ResultObject<>(userAttentionService.cancelAttentionF2F(UserContext.getUserInfo().getId()));
    }


    //删除某用户关注
    @RequestMapping(value = "/attention/delete", method = RequestMethod.POST)
    @ApiSecurityLabel(isCheckLogin = true)
    public ResultObject AttentionDelete(@RequestParam("attention_uid") long attentionUid){
        return new ResultObject<>(userAttentionService.deleteUserAttention(UserContext.getUserInfo().getId(), attentionUid));
    }

    //获取用户关注对应用户信息列表
    @RequestMapping(value = "/attention/userlist")
    @ApiSecurityLabel(isCheckLogin = true)
    public ResultObject<List> AttentionUserList(@RequestParam("attention_uid") long attentionUid,
                                                @RequestParam("last_attention_id") long lastAttentionId,
                                                @RequestParam("page_size") long pageSize){
        if (pageSize > 50 || pageSize < 0) {
            throw new VinciException(ErrorCode.ARGUMENT_ERROR, "传入的页面大小过大或者页面参数错误", "传入的页面大小过大或者页面参数错误");
        }
        return new ResultObject<List>(userAttentionService.getUserAttentionerList(attentionUid, lastAttentionId, pageSize, false));
    }

    //获取用户关注对应用户信息列表
    @RequestMapping(value = "/attention/userlist/both")
    @ApiSecurityLabel(isCheckLogin = true)
    public ResultObject<List> AttentionUserListBoth(@RequestParam("attention_uid") long attentionUid,
                                                @RequestParam("last_attention_id") long lastAttentionId,
                                                @RequestParam("page_size") long pageSize){
        if (pageSize > 50 || pageSize < 0) {
            throw new VinciException(ErrorCode.ARGUMENT_ERROR, "传入的页面大小过大或者页面参数错误", "传入的页面大小过大或者页面参数错误");
        }
        return new ResultObject<List>(userAttentionService.getUserAttentionerList(attentionUid, lastAttentionId, pageSize, true));
    }

    @RequestMapping(value = "/attention/getByName")
    @ApiSecurityLabel(isCheckLogin = true)
    public ResultObject<UserInfo> searchAttentionByName(@RequestParam("nick_name")String nickName) {
        UserInfo own = UserContext.getUserInfo();
        String name = HanLP.convertToPinyinString(nomalizeNickName(nickName),",",false);
        float maxScore = 0.0f;
        long lastUid = 0L;
        UserInfo target = null;
        List<UserAttention> list = userAttentionService.getUserAttentionerList(own.getId(), lastUid, 2000, true);
        for (UserAttention userAttention : list) {
            float score = SimilarityUtil.levenshtein(name, HanLP.convertToPinyinString(userAttention.getUserInfo().getNickNameCheck(), ",", false));
            if (score > 0.8 && score > maxScore) {
                maxScore = score;
                target = userAttention.getUserInfo();
            }
        }
        if (target == null) {
            return new ResultObject<>();
        }
        return new ResultObject<>(target);
    }

    //获取用户粉丝列对应用户信息表
    @RequestMapping(value = "/attention/followlist")
    @ApiSecurityLabel(isCheckLogin = true)
    public ResultObject AttentionFollowList(@RequestParam("attention_uid") long attentionUid,
                                            @RequestParam("last_attention_id") long lastAttentionId,
                                            @RequestParam("page_size") long pageSize){
        return new ResultObject<List>(userAttentionService.getUserFollowerList(attentionUid, lastAttentionId, pageSize));
    }

    //获取用户粉丝列对应用户信息表
    @RequestMapping(value = "/attention/address_book/upload" , method = RequestMethod.POST)
    @ApiSecurityLabel(isCheckLogin = true)
    public ResultObject<Integer> uploadAddressBook(@RequestBody List<UserAddressBook> userAddressBooks){
        return new ResultObject<>(userAttentionService.uploadUserAddressBook(UserContext.getUserInfo().getId(), userAddressBooks));
    }

    //获取用户粉丝列对应用户信息表
    @RequestMapping(value = "/attention/address_book/get")
    @ApiSecurityLabel(isCheckLogin = true)
    public ResultObject<List<UserInfo>> getAddressBook(){
        return new ResultObject<>(userAttentionService.getFriendsFromAddressBook(UserContext.getUserInfo().getId()));
    }

    //获取用户粉丝列对应用户信息表
    @RequestMapping(value = "/attention/address_book/add_friend", method = RequestMethod.POST)
    @ApiSecurityLabel(isCheckLogin = true)
    public ResultObject<Boolean> addFriendInAddressBook(@RequestParam("attention_uid")long attentionUid){
        return new ResultObject<>(userAttentionService.addAttentionUidInAddressBook(UserContext.getUserInfo().getId(), attentionUid));
    }

    //获取用户粉丝列对应用户信息表
    @RequestMapping(value = "/attention/apply_new_friend_list", method = RequestMethod.POST)
    @ApiSecurityLabel(isCheckLogin = true)
    public ResultObject<List<UserAttention>> applyNewFriendList(){
        return new ResultObject<>(userAttentionService.getAttentionApplyMsg(UserContext.getUserInfo().getId()));
    }

}
